package com.js.nickcar.utils;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

import com.zyzd.commontools.string.IDcardUtil;
import com.zyzd.commontools.string.RegexUtil;

import java.text.DecimalFormat;
import java.text.ParseException;

/**
 * Created by 李宗源 on 2016/8/13.
 */
public class StringUtils {
    private static DecimalFormat decimalFormat;

    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(CharSequence input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    public static DecimalFormat getDecimalFormat() {
        if (decimalFormat == null) {
            decimalFormat = new DecimalFormat(".00");
        }
        return decimalFormat;
    }

    public static String formatFloatRetainsOne(double number) {
        return getDecimalFormat().format(number);
    }

    public static String formatPeopleNums(int people) {
        if (people < 10000) {
            return String.format("%d", people);
        } else {
            return String.format("%.2f 万", people / 10000f);
        }
    }

    public static String formatCarNumber(String carNumber) {
        if (TextUtils.isEmpty(carNumber)) return carNumber;
        if (carNumber.length() <= 2) {
            return carNumber.toUpperCase();
        }
        return (carNumber.substring(0, 2) + "·" + carNumber.substring(2, carNumber.length())).toUpperCase();
    }

    public static SpannableStringBuilder indentText(String text) {
        SpannableStringBuilder span = new SpannableStringBuilder("缩进" + text);
        span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return span;
    }

    public static SpannableString getSizeSpan(String content, int fontSize) {
        SpannableString sb = new SpannableString(content);
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(fontSize, true);//设置字体大小 true表示单位是sp
        sb.setSpan(ass, 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }

    /**
     * <pre>
     * 获取账号的加密字符串
     *
     * @param account 账号字符串
     * @return 显示加密后的账号字符串
     *
     * 规则 ：手机号：前3后4
     *       邮箱：前3 完整后缀
     * </pre>
     */
    public static String encryptAlipayAccount(String account) {

        if (TextUtils.isEmpty(account)) return "";

        if (RegexUtil.isMobile(account)) {
            return account.substring(0, 3) + "****" + account.substring(account.length() - 3);
        }

        if (RegexUtil.isEmail(account)) {
            int index = account.lastIndexOf('@');
            String suffix = account.substring(index);

            if (account.length() - suffix.length() <= 3) {
                return "***" + suffix;
            }

            return account.substring(0, 3) + "****" + suffix;
        }

        if (account.length() > 7) {
            return account.substring(0, 3) + "****" + account.substring(account.length() - 3);
        }

        if (account.length() >= 3) {
            return account.substring(0, 1) + "****" + account.substring(account.length() - 1);
        }

        return account;
    }

    /**
     * 获取银行卡尾数（4位）
     *
     * @param bankAccount
     * @return
     */
    public static String encryptBankAccount(String bankAccount) {
        if (TextUtils.isEmpty(bankAccount)) return "";

        String cardNum = bankAccount.trim();

        if (TextUtils.isEmpty(cardNum)) return "";

        if (cardNum.length() > 4) {
            return cardNum.substring(cardNum.length() - 4);
        }
        return cardNum;
    }

    /**
     * 获取加密格式的身份证号
     *
     * @param idCard
     * @return
     */
    public static String encryptIdCard(String idCard) {
        try {
            String checkMsg = IDcardUtil.IDCardValidate(idCard);
            if (!TextUtils.isEmpty(checkMsg)) {//当返回结果为""时表示有效身份证
                return "";
            }
            return idCard.substring(0, idCard.length() == 18 ? 10 : 7) + "****" + idCard.substring(idCard.length() - 4);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String omitStr(String content, int max) {
        if (TextUtils.isEmpty(content) || content.length() <= max) return content;
        if (max <= 1) return content;

        return content.substring(0, max - 1) + "...";
    }

}
